package com.tarena.micro.service.order.service.impl;

import com.alibaba.fastjson2.JSON;
import com.tarena.micro.service.order.feign.CartClient;
import com.tarena.micro.service.order.feign.StockClient;
import com.tarena.micro.service.order.mapper.OrderMapper;
import com.tarena.micro.service.order.service.OrderService;
import com.tarena.micro.service.pojo.message.CartDeleteMessage;
import com.tarena.micro.service.pojo.param.OrderAddParam;
import com.tarena.micro.service.pojo.param.StockReduceParam;
import com.tarena.micro.service.pojo.po.OrderPO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CartClient cartClient;
    @Autowired
    private StockClient stockClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public Boolean addOrder(OrderAddParam param) {
        //1.openFeign调用减库存
        StockReduceParam reduceParam=new StockReduceParam();
        reduceParam.setProductCode(param.getProductCode());
        reduceParam.setCount(param.getCount());
        stockClient.reduceStock(reduceParam);
        //2.本地新增订单
        OrderPO po=new OrderPO();
        BeanUtils.copyProperties(param,po);
        int orderResult=orderMapper.insert(po);
        //3.openFeign调用删除购物车
        //封装消息对象 不存在消息对象
        CartDeleteMessage message=new CartDeleteMessage();
        message.setProductCode(param.getProductCode());
        message.setUserId(param.getUserId());
        rabbitTemplate.convertAndSend("","cart_delete",message);
        //cartClient.deleteCart(param.getProductCode(),param.getUserId());
        return true;
    }
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String detail(Integer id) {
        //1.读取缓存数据 在redis中就使用set get命令 操作String
        String orderKey="order:"+id;//缓存key值 一定是有业务绑定的 订单 用户 商品 充电站场
        //2.判断缓存是否命中,直接读取判断为空
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String orderJson = opsForValue.get(orderKey);
        if (orderJson!=null&&orderJson.length()>0){
            System.out.println("本次查询 缓存返回");
            //缓存命中
            return orderJson;
        }else{
            System.out.println("本次查询 DB返回");
            //3.缓存未命中 读取数据库
            OrderPO orderPO = orderMapper.selectById(id);
            //准备一个即将返回的 从数据库查询到的订单字符串
            String orderDetail="";
            if (orderPO!=null){
                orderDetail= JSON.toJSONString(orderPO);
            }
            //4.返回给调用者之前 存储一个缓存
            opsForValue.set(orderKey,orderDetail);
            return orderDetail;
        }
    }

    public void addOrder(OrderPO po){
        //讨论orderMapper的insert是不是幂等的 调用一次
        orderMapper.insert(po);
        for(int i=0;i<100;i++){

        }

    }
}
